package asia.lics.system.service.impl;

import asia.lics.common.exception.ServiceException;
import asia.lics.system.domain.FUser;
import asia.lics.system.mapper.FUserMapper;
import asia.lics.system.service.IFUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ss-tutor
 * @date 2024-12-03
 */
@Service
public class FUserServiceImpl implements IFUserService {
    @Autowired
    private FUserMapper fUserMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public FUser selectFUserById(Long id) {
        return fUserMapper.selectFUserById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param fUser 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<FUser> selectFUserList(FUser fUser) {
        return fUserMapper.selectFUserList(fUser);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param fUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertFUser(FUser fUser) {
        return fUserMapper.insertFUser(fUser);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param fUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateFUser(FUser fUser) {
        return fUserMapper.updateFUser(fUser);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteFUserByIds(Long[] ids) {
        return fUserMapper.deleteFUserByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteFUserById(Long id) {
        return fUserMapper.deleteFUserById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int vip(Long userId, String num, String unit) {
        FUser fUser = fUserMapper.selectFUserById(userId);
        LocalDateTime vipValid = fUser.getVipValid();
        if (vipValid == null) {
            vipValid = LocalDateTime.now();
        }
        if (vipValid.isBefore(LocalDateTime.now())) {
            vipValid = LocalDateTime.now();
        }
        switch (unit) {
            case "0":
                vipValid = vipValid.plusDays(Integer.parseInt(num));
                break;
            case "1":
                vipValid = vipValid.plusMonths(Integer.parseInt(num));
                break;
            case "2":
                vipValid = vipValid.plusYears(Integer.parseInt(num));
                break;
            default:
                throw new ServiceException("有效期单位错误");
        }
        fUser.setVipValid(vipValid);
        return fUserMapper.updateFUser(fUser);
    }
}
